//
//  CardNumberBindingViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/11.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "CardNumberBindingViewController.h"
#import "LZFoldButton.h"

#import "AFNetworking.h"
@interface CardNumberBindingViewController ()<LZFoldButtonDelegate,UITextFieldDelegate>
{
    LZFoldButton *lzup;
    
//    LZFoldButton *lzdown;

    UITextField *setUpMoneyPasswordTextField;

    UITextField *setUpMoneyPasswordTextField2;

    UITextField *setUpMoneyPasswordTextField3;

    UILabel *cardNumberBindingLabel;

    // 银行数组
    NSMutableArray *arrup;
    // 银行id数组
    NSMutableArray *cnbId;

    NSString *_objStr;
}

@end

@implementation CardNumberBindingViewController

// 银行网络请求
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"Model"];
    [parametersDic setObject:@"GetSystemConfig" forKey:@"Action"];
    [parametersDic setObject:@"HX_Bank_Config" forKey:@"models"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];


        arrup = [[NSMutableArray alloc]init];
        cnbId = [[NSMutableArray alloc]init];
        
        for (NSDictionary *modeldic in [dic objectForKey:@"HX_Bank_Config"]) {
            [cnbId addObject:modeldic[@"B_C_Id"]];
            [arrup addObject:modeldic[@"B_C_Name"]];
        }
         [self cardNumberBindingLZFoldButtonup];
        [cardNumberBindingtableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

// 绑定银行卡网络请求
-(void)obtainData2
{
    if (_objStr != nil) {
        // 启动系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

        //服务器给的域名
        NSString *domainStr = @"http://666.lefun6.com/";

        //创建一个可变字典
        NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
        //往字典里面添加需要提交的参数
        [parametersDic setObject:@"User" forKey:@"Model"];
        [parametersDic setObject:@"set_user_bank_list" forKey:@"Action"];
        [parametersDic setObject:_objStr forKey:@"H_U_B_L_Bank_Id"];
        [parametersDic setObject:setUpMoneyPasswordTextField.text forKey:@"H_U_B_L_Bank_Account"];
        [parametersDic setObject:setUpMoneyPasswordTextField2.text forKey:@"H_U_B_L_Opening_Address"];
        [parametersDic setObject:setUpMoneyPasswordTextField3.text forKey:@"bank_password"];

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];

        //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
        [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 解析成功隐藏系统风火轮
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

//            NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]);
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alerView addAction:action];
            [self presentViewController:alerView animated:YES completion:nil];

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            
        }];
    }
    else{
        UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"请选择银行" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alerView addAction:action];
        [self presentViewController:alerView animated:YES completion:nil];
    }

}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self obtainData];
    self.title = @"卡号绑定";
    self.view.backgroundColor = [UIColor redColor];
    
    self.cardNumberBindingTitles1 = @[@"绑定银行",@"绑定卡号",@"开户地址"];

    
    cardNumberBindingtableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStylePlain];
    cardNumberBindingtableView.dataSource = self;
    cardNumberBindingtableView.delegate = self;
    cardNumberBindingtableView.scrollEnabled = NO;
    [cardNumberBindingtableView registerNib:[UINib nibWithNibName:@"HighLightsTableViewCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
    cardNumberBindingtableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:cardNumberBindingtableView];


    setUpMoneyPasswordTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 20+45+(45-30)/2, UISCREEN_WIDTH-100, 30)];
    setUpMoneyPasswordTextField.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
//    setUpMoneyPasswordTextField.clearButtonMode = YES; //清除模式开启
//    setUpMoneyPasswordTextField.secureTextEntry = YES; //密码输入框
    //协议执行者交给textField自己self
    setUpMoneyPasswordTextField.delegate = self;

    setUpMoneyPasswordTextField2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 20+45*2+(45-30)/2, UISCREEN_WIDTH-100, 30)];
    setUpMoneyPasswordTextField2.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
    //    setUpMoneyPasswordTextField.clearButtonMode = YES; //清除模式开启
    //    setUpMoneyPasswordTextField.secureTextEntry = YES; //密码输入框
    //协议执行者交给textField自己self
    setUpMoneyPasswordTextField2.delegate = self;

    setUpMoneyPasswordTextField3 = [[UITextField alloc]initWithFrame:CGRectMake(100, 20*2+45*3+(45-30)/2, UISCREEN_WIDTH-100, 30)];
    setUpMoneyPasswordTextField3.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
    //    setUpMoneyPasswordTextField.clearButtonMode = YES; //清除模式开启
    //    setUpMoneyPasswordTextField.secureTextEntry = YES; //密码输入框
    //协议执行者交给textField自己self
    setUpMoneyPasswordTextField3.delegate = self;



    [self cardNumberBindingLabel];
}

-(void)cardNumberBindingLZFoldButtonup
{

    lzup = [[LZFoldButton alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH-10-200, 20+(45-30)/2, 200, 30) dataArray:arrup];
    
    lzup.lzDelegate = self;
    lzup.lzFontSize = 12;
    lzup.lzHeight = 300;
    lzup.lzButtonType = 1;
    
    [lzup LZSetTitle:@"请选择银行          " forState:UIControlStateNormal];
    [lzup LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateNormal];
    [lzup LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateSelected];
    [lzup LZSetTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
}

-(void)cardNumberBindingLabel{
    
    cardNumberBindingLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20*2+45*4+1, UISCREEN_WIDTH-10-10, 40)];
    cardNumberBindingLabel.text = @"为了您的资金安全，一经提交，资料无法修改，请谨慎操作!";
    cardNumberBindingLabel.textColor = [UIColor grayColor];
    cardNumberBindingLabel.backgroundColor = [UIColor clearColor];
    cardNumberBindingLabel.font = [UIFont systemFontOfSize:11];
    
    
}

-(void)LZFoldButton:(LZFoldButton*)foldButton didSelectObject:(id)obj {

    NSInteger i = [arrup indexOfObject:obj];
    _objStr = [cnbId objectAtIndex:i];

}


//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
//    }else if (section == 1){
//        return 2;
    }else if (section == 1){
        return 1;
    }else{
        return 1;
    }
}

//设置表视图中的section（分组）数,默认为1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

// 设置header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section < 2) {
        return 20;
    }else{
        return 50;
    }
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45;
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *myCell = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.textLabel.textColor = [UIColor grayColor];
            
    }
    
    //设置数据
    if (indexPath.section == 0) {
        cell.textLabel.text = _cardNumberBindingTitles1[indexPath.row];
    }else if(indexPath.section == 0 & indexPath.row == 0){
      }else if(indexPath.section == 0 & indexPath.row == 1){
      }else if(indexPath.section == 0 & indexPath.row == 2){
//    }else if(indexPath.section == 0 & indexPath.row == 3){
//        setUpMoneyPasswordTextField.hidden = NO;
//    }else if(indexPath.section == 1 & indexPath.row == 0){
//        cell.textLabel.text = @"密保问题";
//        setUpMoneyPasswordTextField.hidden = YES;
//    }else if (indexPath.section == 1 & indexPath.row == 1){
//        cell.textLabel.text = @"问题答案";
//        setUpMoneyPasswordTextField.hidden = NO;
     }else if (indexPath.section == 1){
        cell.textLabel.text = @"取款密码";
     }else{
        cell.textLabel.text = @"设置";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
     }
    
    [cardNumberBindingtableView addSubview:cardNumberBindingLabel];
    [cardNumberBindingtableView addSubview:lzup];
    [cardNumberBindingtableView addSubview:setUpMoneyPasswordTextField];
    [cardNumberBindingtableView addSubview:setUpMoneyPasswordTextField2];
    [cardNumberBindingtableView addSubview:setUpMoneyPasswordTextField3];

    return cell;
    
}

//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == 2 & indexPath.row == 0){
    [self obtainData2];

    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
@end
